package us.icitap.cm.business.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.eclipse.persistence.annotations.Customizer;

import us.icitap.cm.business.entities.regmin.Color;
import us.icitap.cm.business.entities.regmin.VehicleBrand;
import us.icitap.cm.business.entities.regmin.VehicleModel;
import us.icitap.cm.business.entities.regmin.VehicleType;
import us.icitap.cm.utils.MyCustomizer;

@Entity
@Customizer(MyCustomizer.class)
@Table(name="VEHICLE", schema="MPC")
public class Vehicle implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9133335334173501521L;

	

	@Id
	@Column(insertable=false, updatable = false)
	private Integer id;
	

	@OneToOne
	@JoinColumn(name="ID",nullable=false)
	private AppEntity appEntity;

	
	@ManyToOne
	@JoinColumn(name="VEHICLE_TYPE_ID")
	private VehicleType vehicleType;

	@ManyToOne
	@JoinColumn(name="BRAND_ID")
	private VehicleBrand vehicleBrand;
	
	@ManyToOne
	@JoinColumns({@JoinColumn(name = "MODEL_ID", referencedColumnName = "MODEL_ID"), @JoinColumn(name = "BRAND_ID", referencedColumnName = "CODE_PRODUCER",insertable=false, updatable=false)})
	private VehicleModel vehicleModel;
	
	@Column(name="PROD_YEAR")
	private Integer productionYear;
	
	@ManyToOne
	@JoinColumn(name="COLOR_ID")
	private Color color;
	
	
	private String vin;
	
	@Column(name="PLATE_ID")
	private String plateId;
	
	
	@Column(length=500)
	private String details;
	
	@ManyToOne
	@JoinColumn(name="ACTUAL_SITUATION_ID")
	private ActualSituation actualSituation;
	
	
	private boolean registered;
	
	private boolean technicalTested;
	
	private boolean insured;
	
	@Column(length=500)
	private String notes;
	
	public Vehicle() {
		// TODO Auto-generated constructor stub
	}
	
	public Vehicle(Vehicle vehicle) {
		this.actualSituation = vehicle.getActualSituation();
		this.color = vehicle.getColor();
		this.details = vehicle.getDetails();
		this.insured = vehicle.isInsured();
		this.notes = vehicle.getNotes();
		this.plateId = vehicle.getPlateId();
		this.productionYear = vehicle.getProductionYear();
		this.registered = vehicle.isRegistered();
		this.technicalTested = vehicle.isTechnicalTested();
		this.vehicleBrand = vehicle.getVehicleBrand();
		this.vehicleModel = vehicle.getVehicleModel();
		this.vehicleType = vehicle.getVehicleType();
		this.vin = vehicle.getVin();
		
		
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public AppEntity getAppEntity() {
		return appEntity;
	}


	public void setAppEntity(AppEntity appEntity) {
		this.appEntity = appEntity;
	}


	public VehicleType getVehicleType() {
		return vehicleType;
	}


	public void setVehicleType(VehicleType vehicleType) {
		this.vehicleType = vehicleType;
	}


	public VehicleBrand getVehicleBrand() {
		return vehicleBrand;
	}


	public void setVehicleBrand(VehicleBrand vehicleBrand) {
		this.vehicleBrand = vehicleBrand;
	}


	public VehicleModel getVehicleModel() {
		return vehicleModel;
	}


	public void setVehicleModel(VehicleModel vehicleModel) {
		this.vehicleModel = vehicleModel;
	}


	public Integer getProductionYear() {
		return productionYear;
	}


	public void setProductionYear(Integer productionYear) {
		this.productionYear = productionYear;
	}


	public Color getColor() {
		return color;
	}


	public void setColor(Color color) {
		this.color = color;
	}


	public String getVin() {
		return vin;
	}


	public void setVin(String vin) {
		this.vin = vin;
	}


	public String getPlateId() {
		return plateId;
	}


	public void setPlateId(String plateId) {
		this.plateId = plateId;
	}


	public String getDetails() {
		return details;
	}


	public void setDetails(String details) {
		this.details = details;
	}


	public ActualSituation getActualSituation() {
		return actualSituation;
	}


	public void setActualSituation(ActualSituation actualSituation) {
		this.actualSituation = actualSituation;
	}


	public boolean isRegistered() {
		return registered;
	}


	public void setRegistered(boolean registered) {
		this.registered = registered;
	}


	public boolean isTechnicalTested() {
		return technicalTested;
	}


	public void setTechnicalTested(boolean technicalTested) {
		this.technicalTested = technicalTested;
	}


	public boolean isInsured() {
		return insured;
	}


	public void setInsured(boolean insured) {
		this.insured = insured;
	}


	public String getNotes() {
		return notes;
	}


	public void setNotes(String notes) {
		this.notes = notes;
	}


	@Override
	public String toString() {
		if(this.vehicleType != null){
			if(this.vehicleBrand != null)
				if(this.vehicleModel != null) return this.vehicleType.getTag() + ", " + this.vehicleBrand.getTag() + ", " + this.vehicleModel;
				else return this.vehicleType.getTag() + ", " + this.vehicleBrand.getTag();
			else return  this.vehicleType.getTag();
		}
		else if (details != null ) return this.details;
		else return "Automjeti nr. " + id ;

	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((actualSituation == null) ? 0 : actualSituation.hashCode());
		result = prime * result + ((color == null) ? 0 : color.hashCode());
		result = prime * result + ((details == null) ? 0 : details.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + (insured ? 1231 : 1237);
		result = prime * result + ((notes == null) ? 0 : notes.hashCode());
		result = prime * result + ((plateId == null) ? 0 : plateId.hashCode());
		result = prime * result
				+ ((productionYear == null) ? 0 : productionYear.hashCode());
		result = prime * result + (registered ? 1231 : 1237);
		result = prime * result + (technicalTested ? 1231 : 1237);
		result = prime * result
				+ ((vehicleBrand == null) ? 0 : vehicleBrand.hashCode());
		result = prime * result
				+ ((vehicleModel == null) ? 0 : vehicleModel.hashCode());
		result = prime * result
				+ ((vehicleType == null) ? 0 : vehicleType.hashCode());
		result = prime * result + ((vin == null) ? 0 : vin.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vehicle other = (Vehicle) obj;
		if (actualSituation == null) {
			if (other.actualSituation != null)
				return false;
		} else if (!actualSituation.equals(other.actualSituation))
			return false;
		if (color == null) {
			if (other.color != null)
				return false;
		} else if (!color.equals(other.color))
			return false;
		if (details == null) {
			if (other.details != null)
				return false;
		} else if (!details.equals(other.details))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (insured != other.insured)
			return false;
		if (notes == null) {
			if (other.notes != null)
				return false;
		} else if (!notes.equals(other.notes))
			return false;
		if (plateId == null) {
			if (other.plateId != null)
				return false;
		} else if (!plateId.equals(other.plateId))
			return false;
		if (productionYear == null) {
			if (other.productionYear != null)
				return false;
		} else if (!productionYear.equals(other.productionYear))
			return false;
		if (registered != other.registered)
			return false;
		if (technicalTested != other.technicalTested)
			return false;
		if (vehicleBrand == null) {
			if (other.vehicleBrand != null)
				return false;
		} else if (!vehicleBrand.equals(other.vehicleBrand))
			return false;
		if (vehicleModel == null) {
			if (other.vehicleModel != null)
				return false;
		} else if (!vehicleModel.equals(other.vehicleModel))
			return false;
		if (vehicleType == null) {
			if (other.vehicleType != null)
				return false;
		} else if (!vehicleType.equals(other.vehicleType))
			return false;
		if (vin == null) {
			if (other.vin != null)
				return false;
		} else if (!vin.equals(other.vin))
			return false;
		return true;
	}
	
	public boolean myEquals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vehicle other = (Vehicle) obj;
		if (actualSituation == null) {
			if (other.actualSituation != null)
				return false;
		} else if (!actualSituation.equals(other.actualSituation))
			return false;
		if (color == null) {
			if (other.color != null)
				return false;
		} else if (!color.equals(other.color))
			return false;
		if (details == null) {
			if (other.details != null)
				return false;
		} else if (!details.equals(other.details))
			return false;
		
		if (insured != other.insured)
			return false;
		if (notes == null) {
			if (other.notes != null)
				return false;
		} else if (!notes.equals(other.notes))
			return false;
		if (plateId == null) {
			if (other.plateId != null)
				return false;
		} else if (!plateId.equals(other.plateId))
			return false;
		if (productionYear == null) {
			if (other.productionYear != null)
				return false;
		} else if (!productionYear.equals(other.productionYear))
			return false;
		if (registered != other.registered)
			return false;
		if (technicalTested != other.technicalTested)
			return false;
		if (vehicleBrand == null) {
			if (other.vehicleBrand != null)
				return false;
		} else if (!vehicleBrand.equals(other.vehicleBrand))
			return false;
		if (vehicleModel == null) {
			if (other.vehicleModel != null)
				return false;
		} else if (!vehicleModel.equals(other.vehicleModel))
			return false;
		if (vehicleType == null) {
			if (other.vehicleType != null)
				return false;
		} else if (!vehicleType.equals(other.vehicleType))
			return false;
		if (vin == null) {
			if (other.vin != null)
				return false;
		} else if (!vin.equals(other.vin))
			return false;
		return true;
	}
	
}
